﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Process
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

if(system.platform=="windows") {
myPlugins["process"] = new Object();
myPlugins["process"]["name"] = getLocalizedString2("mod_sysm_plugin_process_name");
myPlugins["process"]["onlyonce"] = true;
myPlugins["process"]["newObject"] = function (NewSettings) { return new plugin_process_sensor(NewSettings); }
}

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_process_sensor(NewSettings) {
	// Required variables
	this.plugin = "process";
	this.settings = NewSettings;
	this.edit = plugin_process_edit;
	this.ReDraw = plugin_process_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	//this.frame.style.backgroundColor = "red";

	this.ProcessList = new Object();

	// Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.updspeed) this.settings.updspeed = 5;
	if(!this.settings.warning) {
		alert(getLocalizedString2("mod_sysm_plugin_process_warning"));
		this.settings.warning = true
	}


	// The Sensore
	this.process = new class_TextLine(this.frame,0,getLocalizedString2("mod_sysm_plugin_process_label1"));
	this.process.label.color = this.settings.color;
	this.process.data.color = this.settings.color;
	this.process.data.truncation = "end";
	this.processAnimation = new FadeAnimation( this.process.data, 255, 200, animator.kEaseIn );

	this.processmem = new class_TextLine(this.frame,mod_sysmonitor_size,getLocalizedString2("mod_sysm_plugin_process_label2"));
	this.processmem.label.color = this.settings.color;
	this.processmem.data.color = this.settings.color;
	this.processmem.data.truncation = "end";
	this.processmemAnimation = new FadeAnimation( this.processmem.data, 255, 200, animator.kEaseIn );


	// Update Timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_process_refresh.call(obj); };
	plugin_process_refresh.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.process.data.vOffset + (this.process.data.size * 1/3);
	this.frame.height = this.processmem.data.vOffset + (this.processmem.data.size * 1/3);
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_process_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(1,2,3,5,10,15,20,25,30);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.process.label.color = this.settings.color;
			this.process.data.color = this.settings.color;

			if ( this.settings.updspeed != Drive_Edit_DialogResults[1] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[1];
				this.timer.interval = this.settings.updspeed;
			}

			plugin_process_refresh.call(obj);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_process_refresh() {
		// Animate update
		this.process.data.opacity = 96;
		this.processmem.data.opacity = 96;
		updateNow();

		var ProcessListName;
		if(VBSEnabled) ProcessListName = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_process + '"').split("\n");
		else ProcessListName = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "Name" "1" "SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE Name <> \"_Total\""').split("\n");

		for(var i in this.ProcessList)
			this.ProcessList[i].cpu = 0;

		var tmp;
		var peak = "";
		var peakValue = -1;
		for(var i = 0 ; i < ProcessListName.length ; i++ ) {
			tmp = plugin_process_GetCPU(ProcessListName[i]);
			if(!this.ProcessList[ProcessListName[i]]) {
				this.ProcessList[ProcessListName[i]] = new Object();
				this.ProcessList[ProcessListName[i]].cpu = 0;
				this.ProcessList[ProcessListName[i]].last = tmp;
			}
			this.ProcessList[ProcessListName[i]].cpu = tmp - this.ProcessList[ProcessListName[i]].last;
			this.ProcessList[ProcessListName[i]].last = tmp;

			if(this.ProcessList[ProcessListName[i]].cpu > peakValue && ProcessListName[i] != "Idle") {
				peakValue = this.ProcessList[ProcessListName[i]].cpu;
				peak = ProcessListName[i];
			}
		}

		var mempeak = "";
		var mempeakValue = -1;
		var ProcessListName = runCommand('tasklist /FO CSV /NH').split("\n");
		for(var i = 1 ; i < ProcessListName.length ; i++ ) {
			tmp = ProcessListName[i].split(",");
			j = parseInt(tmp[4].substring(1, tmp[4].length - 3).replace(/\./g, "")) * 1024;
			if(j > mempeakValue) { mempeakValue = j; mempeak = tmp[0].substring(1, tmp[0].length - 5); }
		}
		

		//this.process.data.data = peak;
		this.process.data.data = Math.round( peakValue / this.timer.interval / 100000 / 2 ) + "% " + peak;
		this.processmem.data.data = bytesToUIString( mempeakValue ) + " " + mempeak;

		// Animate update
		animator.start( this.processAnimation ) ;
		animator.start( this.processmemAnimation ) ;

		return true;
	}

	function plugin_process_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.process.size = mod_sysmonitor_size;
		this.frame.height = this.process.data.vOffset + (this.process.data.size * 1/3);
		return true;
	}

	function plugin_process_GetCPU(ProcessName) {
		return wmi.Get( 'Win32_PerfRawData_PerfProc_Process.name="' + ProcessName + '"' ).Properties_.Item( "PercentProcessorTime" ).Value
	}

	function plugin_process_GetMEM(ProcessName) {
		// WorkingSet = Phy.Ram ???

		return wmi.Get( 'Win32_PerfFormattedData_PerfProc_Process.name="' + ProcessName + '"' ).Properties_.Item( "VirtualBytes" ).Value
	}

}

// --------------------------------------------------------------------------------
// The END